<?php

namespace App\Providers;

use Laravel\Telescope\EntryType;
use Laravel\Telescope\Telescope;
use Illuminate\Support\Facades\Gate;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\TelescopeApplicationServiceProvider;

class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $allowIp = [
            '45.117.97.64',
            '61.61.134.118',
            '127.0.0.1'
        ];

        Telescope::filter(function (IncomingEntry $entry) use ($allowIp) {

            if ($entry->type == EntryType::REQUEST) {
                $entry->tags([
                    'ip:' . ip(),
                    'response_status:' . ($entry->content['response_status'] ?? 200),
                ]);
            }

            return app()->environment('dev', 'develop')
                || in_array(ip(), $allowIp)
                || in_array($entry->content['response_status'] ?? 200, [404, 405])
                || $entry->isFailedRequest()
                || $entry->isReportableException()
                || $entry->isFailedJob()
                || $entry->isScheduledTask()
                || $entry->hasMonitoredTag();
        });
    }

    /**
     * Prevent sensitive request details from being logged by Telescope.
     *
     * @return void
     */
    protected function hideSensitiveRequestDetails()
    {
        return;
    }

    /**
     * Register the Telescope gate.
     *
     * This gate determines who can access Telescope in non-local environments.
     *
     * @return void
     */
    protected function gate()
    {
        Gate::define('viewTelescope', function () {

            return in_array(ip(), [
                '45.117.97.64',
                '61.61.134.118',
                '127.0.0.1'
            ]);
        });
    }
}
